package com.intelligentclass.controller;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.intelligentclass.constant.UserType;
import com.intelligentclass.entity.Student;
import com.intelligentclass.entity.Teaching;
import com.intelligentclass.exception.UserTypeException;
import com.intelligentclass.mydata.CourseInfo;
import com.intelligentclass.mydata.MyJson;
import com.intelligentclass.services.CourseService;
import com.intelligentclass.services.StudentService;
import com.intelligentclass.services.TeacherService;
import com.intelligentclass.services.TeachingService;
import com.intelligentclass.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class StudentController {

    private final CourseService courseService;
    private final TeacherService teacherService;
    private final StudentService studentService;
    private final TeachingService teachingService;

    @Autowired
    public StudentController(StudentService studentService, TeachingService teachingService, CourseService courseService, TeacherService teacherService) {
        this.studentService = studentService;
        this.teachingService = teachingService;
        this.courseService = courseService;
        this.teacherService = teacherService;
    }

    /**
     *
     * @param header
     *      must contain a valid token used to identify if the user is valid
     * @return
     */
    @GetMapping("/api/student/getClassList")
    public ResponseEntity<?> getClassList(@RequestHeader Map<String, String> header) {

        String token = header.get("token");
        MyJson json = new MyJson();
        DecodedJWT jwt;
        try {
            jwt = JWTUtil.verifyToken(token);
            int userType = jwt.getClaim("usertype").asInt();
            if(userType != UserType.STUDENT) {
                throw new UserTypeException();
            }
        }
        catch(UserTypeException e){
            json.setMessage("Not enough access permissions");
            json.setStatus(401);
            return ResponseEntity.ok().body(json.toString());
        }
        catch (TokenExpiredException e) {
            json.setMessage("Token is expired");
            json.setStatus(400);
            json.addData("status", 0);
            return ResponseEntity.ok().body(json);
        }
        catch (Exception e) {
            System.exit(1);
            return null;
        }

        int userType = jwt.getClaim("usertype").asInt();
        String username = jwt.getClaim("username").asString();

        //check if it is student
        if(userType == UserType.STUDENT){
            Student stu = studentService.searchBySno(username);
            List<Teaching> teachings = stu.getCourseList();
            List<CourseInfo> courses = new ArrayList<>();

            //get all classes of this student
            for(Teaching teaching : teachings){
                String tno = teaching.getTno();
                String cno = teaching.getCno();
                int cid = teaching.getCid();
                CourseInfo c = new CourseInfo();
                c.setCname(courseService.getCnameByCno(cno));
                c.setTname(teacherService.getTnameByTno(tno));
                c.setCid(cid);
                c.setCno(cno);
                courses.add(c);
            }

            json.setMessage("Successfully get course list");
            json.setStatus(200);
            json.addData("status", 1);
            json.addData("courses", courses);
            return ResponseEntity.ok().body(json);
        }

        return null;
    }
}
